package com.example.taobaounion3.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.taobaounion3.R;
import com.example.taobaounion3.base.baseFragment;
import com.example.taobaounion3.model.domain.Categories;
import com.example.taobaounion3.presenter.IHomePresenter;
import com.example.taobaounion3.presenter.impl.HomePresenterlmpl;
import com.example.taobaounion3.ui.adapter.HomePagerAdapter;
import com.example.taobaounion3.utils.LogUtils;
import com.example.taobaounion3.utils.PresenterManager;
import com.example.taobaounion3.view.IHomeCallback;
import com.google.android.material.tabs.TabLayout;

import butterknife.BindView;

public class HomeFragment extends baseFragment implements IHomeCallback {

    private IHomePresenter mHomePresenter;

    @BindView(R.id.home_pager)
    public ViewPager mHomePager;

    @BindView(R.id.home_indicator)
    public TabLayout mTabLayout;

    private HomePagerAdapter mHomePagerAdapter;

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.base_home_fragment_layout,container,false);
    }

    @Override
    protected void initView(View rootView) {
        mTabLayout.setupWithViewPager(mHomePager);
        //给ViewPager设置适配器
       mHomePagerAdapter=new HomePagerAdapter(getChildFragmentManager());
       mHomePager.setAdapter(mHomePagerAdapter);
    }

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initpresenter() {
        //创建presenter
         mHomePresenter= PresenterManager.getInstance().getmHomePresenter();
         mHomePresenter.registerViewCallback(this);
    }

    @Override
    protected void loadDate() {
        setUpState(State.LOADING);
        //加载数据
        mHomePresenter.getCategories();
    }

    @Override
    public void onCategoriesLoaded(Categories categories) {
        setUpState(State.SUCCESS);
        LogUtils.d(this,"onCategoriesLoaded..");
        //加载的数据会从这里回来
        if (mHomePagerAdapter!=null) {
            //ViewPager的预加载
            //mHomePager.setOffscreenPageLimit(categories.getData().size());
            mHomePagerAdapter.setCategories(categories);
        }
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setUpState(State.EMPTY);
    }

    @Override
    protected void release() {
        //取消回调注册
        if(mHomePresenter!=null){
            mHomePresenter.unregisterViewCallback(this);
        }
    }

    @Override
    protected void onRetryClick() {
        //点击重新加载分类
        if (mHomePresenter!=null){
            mHomePresenter.getCategories();
        }
    }
}
